<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use think\Exception;
use XznsTools\cache\CacheManage;

class User extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_user';

    /**
     * 准了号 用户搜索
     * @param int $uid
     * @param string $uen 准了号
     * @param string $field 过滤字段
     * @return array
     */
    public function uenSearch(int $uid, string $uen, $field = '*')
    {
        try
        {
            $model = $this->where('uen', $uen);

            if ($uid)
            {
                $model = $model->whereNotIn('id', $uid);
            }
            // 获取用户信息
            $userInfo = $model->field($field)->find();
            if (!$userInfo || (new ZlUserZx())->where('uid', $userInfo['id'])->count()) return [];
        }
        catch (Exception $e)
        {
            return [];
        }

        return $userInfo;
    }

    /**
     * 通过准了号获取用户uid
     * @param string $uen
     * @return int
     * @throws \Exception
     */
    public function getUidByUen(string $uen)
    {
        $uid = CacheManage::getInstance()->setCacheConfig('user.uen2uid', $uen)
            ->getDataAndSet(function () use ($uen)
        {
            try
            {
                // 获取用户uid
                return $this->where('uen', $uen)->value('id');
            }
            catch (\Exception $e)
            {
                return 0;
            }
        });

        return (int)$uid;
    }
}
